﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Jitter.LinearMath;
using Ether.Libs; 

namespace Ether.Mapping
{
    /// <summary>
    /// Represents the Position in the Ether (Tile Space)
    /// </summary>
    public struct EtherPosition {
        public int X;
        public int Z;
        public int S;

        public EtherPosition(int x, int z, int s) {
            this.X = x;
            this.Z = z;
            this.S = s;
        }

        public Vector3 ToVector3()
        {
            return new Vector3(X, S, Z);
        }
        public JVector ToJVector()
        {
            return new JVector(X, S, Z);
        }

        public Vector3 ToWorldSpace()
        {
            return (ToVector3() * new Vector3(EtherEngine.TileSize.X, EtherEngine.FloorHeight, EtherEngine.TileSize.Z))
                + (EtherEngine.TileSize / 2);
        }
        public JVector ToConvertedWorldSpace()
        {
            return Conversion.ConvertVect(ToVector3() * EtherEngine.TileSize);
        }
        
        public static EtherPosition FromMapPosition(Vector2 mapPosition, int level)
        {
            EtherPosition pos = new EtherPosition();

            pos.X = (int)mapPosition.X;
            pos.Z = (int)mapPosition.Y;
            pos.S = level;

            return pos;
        }

        public static EtherPosition operator + (EtherPosition one, EtherPosition two) {
            return new EtherPosition(one.X + two.X, one.Z + two.Z, one.S + two.S);
        }
        public static EtherPosition operator +(EtherPosition one)
        {
            return new EtherPosition(+one.X, +one.Z, one.S);
        }
        
        public static EtherPosition operator - (EtherPosition one, EtherPosition two)
        {
            return new EtherPosition(one.X - two.X, one.Z - two.Z, one.S - two.S);
        }        
        public static EtherPosition operator -(EtherPosition one)
        {
            return new EtherPosition(-one.X, -one.Z, one.S);
        }

        public override string ToString()
        {
            return String.Format("X:{0}, Z:{1}, S:{2}", this.X, this.Z, this.S);
        }

        public static EtherPosition Zero()
        {
            return new EtherPosition(0, 0, 0);
        }
        public static EtherPosition One()
        {
            return new EtherPosition(1, 1, 1);
        }
    }
}
